home *** CD-ROM | disk | FTP | other *** search
/ internet.au CDrom 42 / NETCD42.iso / web / w95 / dream2.exe / data1.cab / Program_Files / Configuration / Commands / Format Table.js < prev    next >
Encoding:
JavaScript  |  1998-11-30  |  14.7 KB  |  411 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. // *************** GLOBAL VARS  *****************
  4.  
  5. //these constants used in tableFormats.js file when defining new table formats
  6. var NONE=0,LEFT=1,CENTER=2,RIGHT=3,BOLD=1,ITALIC=2,BOLD_ITALIC=3;
  7. var ORIGINAL_PREVIEW_TABLE = getPreviewTable().outerHTML;
  8.  
  9. //******************* API **********************
  10.  
  11. function commandButtons(){
  12.   var tableObj = findTable();
  13.   var Buttons=new Array(BUTTON_OK,        "applyFormatToSelectedTable();window.close();",
  14.                         BUTTON_Apply,     "applyFormatToSelectedTable()",
  15.                         BUTTON_Cancel,    "window.close()",
  16.                                                 BUTTON_Help,      "displayHelp()");
  17.   if (isUnrecognizedTable(tableObj) || hasCaption(tableObj))
  18.     Buttons=new Array(BUTTON_OK,"window.close()",
  19.                       BUTTON_Cancel,"window.close()");
  20.  
  21.   return Buttons;
  22. }
  23.  
  24. function canAcceptCommand(){
  25.   if (findTable())
  26.     return true;
  27.   else
  28.     return false;
  29. }
  30.  
  31.  
  32. //***************** LOCAL FUNCTIONS  ******************
  33.  
  34. function findTable(){
  35.   var tableObj="";
  36.   var selArr = dreamweaver.getSelection();
  37.   var selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]);
  38.  
  39.   while (tableObj=="" && selObj.parentNode){
  40.     if (selObj.nodeType == Node.ELEMENT_NODE && selObj.tagName=="TABLE")
  41.       tableObj=selObj;
  42.     else
  43.       selObj = selObj.parentNode;
  44.   }
  45.   return tableObj;
  46. }
  47.  
  48. function updatePreview(){
  49.   alternateRows(findObject("presetNames").selectedIndex,getPreviewTable());
  50. }
  51.  
  52. function removeTag( theObj, Tag )
  53. {
  54.     var children   = theObj.childNodes;
  55.     var nChildren  = children.length;
  56.  
  57.     for( var i = 0; i < nChildren; i++ )
  58.     {
  59.          var currentChild = children.item(i);
  60.  
  61.          if ( currentChild.hasChildNodes() )
  62.             removeTag( currentChild, Tag );
  63.  
  64.          if ( currentChild.nodeType == Node.ELEMENT_NODE &&
  65.               currentChild.tagName == Tag )
  66.               currentChild.outerHTML = currentChild.innerHTML;
  67.      }
  68. }
  69.  
  70.  
  71. function applyFormatToSelectedTable(){
  72.  
  73.   var selObj,selArr;
  74.   
  75.   //get current selection
  76.   selArr = dreamweaver.getSelection();
  77.   selObj = dreamweaver.offsetsToNode(selArr[0],selArr[1]); 
  78.   alternateRows(findObject("presetNames").selectedIndex,findTable());
  79.   
  80.   //restore original selection
  81.   selArr = dreamweaver.nodeToOffsets(selObj);
  82.   dreamweaver.setSelection(selArr[0],selArr[1]);
  83.  
  84. }
  85.  
  86. function isUnrecognizedTable(tableObj) { //checks selected table
  87.   var counter=0;
  88.   var trIter = tableObj.childNodes;
  89.   var trNode=trIter.item(counter);
  90.   var retVal=false;
  91.   while (trNode && !retVal) {
  92.     if (trNode.tagName!="TR" || trNode.childNodes.length==0) 
  93.       retVal=true;
  94.     else trNode=trIter.item(++counter)
  95.   }
  96.   return retVal;
  97. }
  98.  
  99. function hasCaption(theObj){
  100.   if (theObj.childNodes.item(0).tagName && theObj.childNodes.item(0).tagName=="CAPTION")
  101.     return true;
  102.   return false;
  103. }
  104.  
  105. function initializeUI() { //fill presetNames list
  106.   var mainLayer=findObject("mainLayer");
  107.   var presetNamesList,nSize,Names,i;
  108.   var tableObj=findTable();
  109.  
  110.  if (hasCaption(tableObj))
  111.      mainLayer.innerHTML="<p> </p>" + MSG_CaptionIsPresent;
  112.  else if (isUnrecognizedTable(tableObj))
  113.     mainLayer.innerHTML="<p> </p>" + MSG_IsInvalidTable;
  114.  else {
  115.       //add select list options
  116.       mainLayer.visibility="hidden";
  117.       for (i=0;i<4;i++){  //populate select lists
  118.         findObject("topRowAlign").options[i] = new Option(OPTIONS_Align[i]);
  119.         findObject("topRowTextStyle").options[i]=new Option(OPTIONS_Text_Style[i]);
  120.         findObject("leftColAlign").options[i] = new Option(OPTIONS_Align[i]);
  121.         findObject("leftColTextStyle").options[i] = new Option(OPTIONS_Text_Style[i]);
  122.         findObject("rowLimit").options[i] = new Option(OPTIONS_Row_Limit[i]);
  123.       }
  124.       findObject("rowLimit").options[4] = new Option(OPTIONS_Row_Limit[4]);
  125.  
  126.     presetNamesList = findObject("presetNames");
  127.     Names=tableFormats();  //get list of table format names from tableFormats.js file
  128.     nSize = Names.length;
  129.     for (i=0;i<nSize;i++) { 
  130.       presetNamesList.options[i] = new Option(Names[i].name); //populate form field
  131.     }
  132.     findObject("mainLayer").visibility="visible";
  133.     setUI(7); //choose green and yellow format to start with
  134.     alternateRows(7,getPreviewTable()); //apply this format to preview table
  135.     presetNamesList.selectedIndex=7; //select green & yellow format
  136.   
  137.  
  138.  } 
  139. }
  140.  
  141.  
  142. function alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor){
  143.   var rowLen=trIter.length;
  144.   var cellLen,tdNode,trNode,counter;
  145.   if (rowLimit==0) rowLimit=1000; //repeat first color if rowLimit is equal to 0 
  146.   var counter=2*rowLimit;
  147.   var startRow = (topRowColor)?1:0;
  148.   for (i=startRow;i<rowLen;i++){
  149.     trNode=trIter.item(i);
  150.     rowColor=(counter%(2*rowLimit)<rowLimit)?firstRowColor:secondRowColor;
  151.     if (!useTD) //if bgcolor attribute is attached to tr tags
  152.       trNode.setAttribute("bgcolor",rowColor);
  153.     else { //else attach bgcolor attribute to td tags
  154.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  155.       for (j=0;j<cellLen;j++) 
  156.         tdIter.item(j).setAttribute("bgcolor",rowColor);
  157.     }
  158.     counter++; 
  159.   }
  160. }
  161.  
  162. function formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD){
  163.   var trNode=trIter.item(0), cellLen;
  164.  
  165.   //add bgcolor & align
  166.   if (!useTD) { //if attaching bgcolor & align to first row TR tag
  167.     if (topRowColor)
  168.       trNode.setAttribute("bgcolor",topRowColor);
  169.     if (!topRowAlign)
  170.       trNode.removeAttribute("align");
  171.     else
  172.       trNode.setAttribute("align",topRowAlign);
  173.   } else { //else attach bgcolor & align to TD tags in first row
  174.     tdIter=trNode.childNodes; cellLen=tdIter.length;
  175.     for (i=0;i<cellLen;i++){
  176.      tdNode=tdIter.item(i);
  177.      if (topRowColor)
  178.        tdNode.setAttribute("bgcolor",topRowColor);
  179.      if (!topRowAlign)
  180.        tdNode.removeAttribute("align");
  181.      else
  182.        tdNode.setAttribute("align",topRowAlign);        
  183.     }
  184.   } 
  185.  
  186.   //add text color and text formatting
  187.   tdIter=trNode.childNodes; cellLen=tdIter.length;
  188.    for (i=0;i<cellLen;i++) {
  189.      tdNode=tdIter.item(i);
  190.      //set font color, if specified
  191.      if (topRowTextColor){
  192.        if (!findTag(tdNode,"FONT")){ //if the color attribute cannot be added to a "safe" font tag, add it.
  193.          tdNode.innerHTML="<font color='" + topRowTextColor + "'>" + tdNode.innerHTML + "</font>";
  194.        }
  195.        else  { //font tag already exists, set color attribute on it
  196.          findTag(tdNode,"FONT").setAttribute("color",topRowTextColor);
  197.        }
  198.      }
  199.       //set text style
  200.      if (topRowTextStyle==0){//if text style set to none
  201.        removeTag(tdNode,"B"); //remove bold tags
  202.        removeTag(tdNode,"I"); //and remove italic tags
  203.      } else {
  204.        if (topRowTextStyle==1 || topRowTextStyle==3){ //if bold or bold italic
  205.          if (topRowTextStyle==1) //if bold
  206.            removeTag(tdNode,"I"); //remove all italic tags
  207.          if (!findTag(tdNode,"B")){ //if a B tag is not found around the table cell text
  208.            removeTag(tdNode,"B"); //remove any other B tags
  209.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  210.          }
  211.        }
  212.        if (topRowTextStyle==2 || topRowTextStyle==3){ //if italic or or bold italic
  213.          if (topRowTextStyle==2) //if italic
  214.            removeTag(tdNode,"B"); //remove all bold tags
  215.          if (!findTag(tdNode,"I")){ //if an I tag is not found around the table cell text 
  216.            removeTag(tdNode,"I"); //remove any other I tags
  217.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child an I tag
  218.          }
  219.        }
  220.      }
  221.    } 
  222. }
  223.  
  224. function formatLeftCol(trIter,leftColTextStyle,leftColAlign){
  225.   rowLen=trIter.length;
  226.    for (i=0;i<rowLen;i++){
  227.      trNode=trIter.item(i);
  228.      tdNode=trNode.childNodes.item(0);
  229.      //set text style
  230.      if (leftColTextStyle==0){ //if text style set to none
  231.        removeTag(tdNode,"B"); //remove bold tags
  232.        removeTag(tdNode,"I"); //and remove italic tags
  233.      }
  234.      else {
  235.        if (leftColTextStyle==1 || leftColTextStyle==3){ //if text style set to bold or bold italic
  236.          if (leftColTextStyle==1) //if bold
  237.            removeTag(tdNode,"I"); //remove all italic tags
  238.          if (!findTag(tdNode,"B")){ //if B tag is not found around text
  239.            removeTag(tdNode,"B"); //remove all other B tags
  240.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  241.          }
  242.     
  243.        }
  244.        if (leftColTextStyle==2 || leftColTextStyle==3){ //if text style is set to italic or bold italic
  245.          if (leftColTextStyle==2) //if italic
  246.            removeTag(tdNode,"B"); //remove all bold tags
  247.          if (!findTag(tdNode,"I")){ //if i tag is not found around text
  248.            removeTag(tdNode,"I"); //remove all other i tags
  249.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child tag i tag
  250.          }
  251.        }
  252.      }     
  253.      //set alignment
  254.      if (!leftColAlign)
  255.        tdNode.removeAttribute("align");
  256.      else
  257.        tdNode.setAttribute("align",leftColAlign);        
  258.    }
  259. }
  260.  
  261. function setUI(presetIndex){
  262.       var Names = tableFormats();
  263.       var thisFormat = Names[presetIndex];               
  264.       with (thisFormat){
  265.         //error check rowLimit value
  266.         if (rowLimit>4 || rowLimit<0)rowLimit=1;
  267.     
  268.         //align attributes are specified as "left","center","right", and "";
  269.         //following function gets correct selected index for alignment option
  270.         topRowAlign=getIndex(topRowAlign);
  271.         leftColAlign=getIndex(leftColAlign); 
  272.         //select the appropriate options
  273.         findObject("rowLimit").selectedIndex=parseInt(rowLimit);
  274.         findObject("topRowAlign").selectedIndex=topRowAlign;
  275.         findObject("topRowTextStyle").selectedIndex=topRowTextStyle;
  276.         findObject("leftColAlign").selectedIndex=leftColAlign;
  277.         findObject("leftColTextStyle").selectedIndex=leftColTextStyle;
  278.       
  279.         //fill in textfields
  280.         findObject("firstRowColor").value = firstRowColor;
  281.         findObject("secondRowColor").value = secondRowColor;
  282.         findObject("topRowColor").value = topRowColor;
  283.         findObject("topRowTextColor").value = topRowTextColor;
  284.         findObject("borderSize").value = border;
  285.       }
  286.  
  287. }
  288. function alternateRows(presetChoiceIndex,tableObj){
  289.   var tableNode,trIter,trNode,tdIter,tdNode,counter;
  290.   var useTD,topRowColor,selInd,topRowAlign,topRowTextColor;
  291.   var topRowTextStyle,firstRowColor,secondRowColor;
  292.   var rowLimit,borderSize;
  293.  
  294.   tableNode=tableObj;
  295.   trIter=tableNode.childNodes;
  296.   trNode,tdIter,tdNode;
  297.   useTD=findObject('useTD').checked?true:false;
  298.   
  299.   //The rest of the function assigns values to below variables based on user interface.
  300.   //User interface is initially populated with argument values, but the user can change
  301.   //them to dynamically update the preview table.
  302.  
  303.   //set variables for the top row
  304.   topRowColor=findObject('topRowColor').value;
  305.   selInd = findObject('topRowAlign').selectedIndex;
  306.   topRowAlign=findObject('topRowAlign').options[selInd].value;
  307.   topRowTextColor=findObject('topRowTextColor').value;
  308.   topRowTextStyle=findObject('topRowTextStyle').selectedIndex;
  309.   //set variables for the left col
  310.   selInd = findObject('leftColAlign').selectedIndex;
  311.   leftColAlign=findObject('leftColAlign').options[selInd].value;
  312.   leftColTextStyle=findObject('leftColTextStyle').selectedIndex;
  313.   //set variables for the row Colors
  314.   firstRowColor=findObject('firstRowColor').value;
  315.   secondRowColor=findObject('secondRowColor').value;
  316.   rowLimit=findObject("rowLimit").selectedIndex;
  317.   //set border size
  318.   borderSize=findObject("borderSize").value;
  319.  
  320.   //Now, use these values to format the table...
  321.   
  322.   //set table border
  323.   tableNode.setAttribute("border",borderSize); 
  324.  
  325.   //alternate row Colors  
  326.   alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor);
  327.  
  328.   //add left col formatting:text style & alignment
  329.   formatLeftCol(trIter,leftColTextStyle,leftColAlign);
  330.  
  331.   //add top row formatting:text style,alignent,row color, & text color
  332.   formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD);
  333.   
  334.   //set border
  335.   tableNode.setAttribute("border",borderSize);
  336.  
  337. }
  338.  
  339. function updatePreviewTable(){
  340.   var presetIndex = findObject("presetNames").selectedIndex;
  341.   var previewTable = getPreviewTable();
  342.   var mainLayer = findObject("mainLayer");
  343.   
  344.   mainLayer.visiblity = "hidden";
  345.   previewTable.outerHTML = ORIGINAL_PREVIEW_TABLE;; //remove prior formatting of preview table
  346.   setUI(presetIndex);
  347.   alternateRows(findObject("presetNames").selectedIndex,previewTable);//format table
  348.   mainLayer.visibility = "visible";
  349. }
  350.  
  351. function getPreviewTable(){
  352.    //returns preview Table object
  353.    return document.getElementsByTagName("TABLE").item(1);
  354. }
  355.  
  356.  
  357. function getIndex(align){
  358.   //returns align index of UI that matches text of align argument i.e: "center"
  359.   //Note: I didn't use constants here because it is more intuitive to define
  360.   //the alignment attribute following the html syntax of align="attribute"
  361.     switch (align){
  362.      case "left": align=1; break;
  363.      case "center": align=2; break;
  364.      case "right": align=3; break;
  365.      default: align=0; break;
  366.    }
  367.  return align;
  368. }
  369.  
  370. //searches the child nodes of tableCellObj -
  371. //returns the innermost object of tagName that surrounds all of the text in
  372. //the cell. returns empty string if not found.
  373. function findTag(obj, tag) {
  374.   var retVal="";
  375.   while (obj.childNodes && obj.childNodes.length == 1) {
  376.     obj = obj.childNodes.item(0);
  377.    if (obj.nodeType == Node.ELEMENT_NODE && obj.tagName == tag)
  378.      retVal=obj;
  379.   }
  380.   return retVal;
  381.  
  382. }
  383.  
  384.  
  385. //**************** GENERIC FUNCTIONS ****************
  386.  
  387. function findObject(objName,  parentObj) {
  388.   var i,tempObj="",found=false,curObj = "";
  389.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  390.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  391.   else {
  392.     parentObj = (parentObj != null)? parentObj.document : document;
  393.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  394.     else { //if in form
  395.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  396.         if (parentObj.forms[i][objName]) {
  397.           curObj = parentObj.forms[i][objName];
  398.           found = true; break;
  399.       } }
  400.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  401.         parentObj = parentObj.layers;
  402.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  403.           tempObj = findObject(objName,parentObj[i]); //recurse
  404.           if (tempObj) { curObj = tempObj; break;} //if found, done
  405.   } } } }
  406.   return curObj;
  407. }
  408.  
  409.  
  410.  
  411.